
<template>
  <div class="app-container">

    <div class="filter-container">
      
      <el-button v-if="this.listQuery.nodeId != undefined && this.total == 0" class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        创建采纳报告书
      </el-button>
     
     </div> 

     <div style="color:red; font-weight: 700;">采纳报告书</div>

    <el-table ref="dragTable" v-loading="listLoading" :data="list" row-key="id" border fit highlight-current-row style="width: 100%">
      <el-table-column min-width="100px" label="编号">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span></template>
      </el-table-column>
      <el-table-column min-width="100px" label="节点范围">
        <template slot-scope="scope">
          <span>{{ scope.row.nodeRange }}</span></template>
      </el-table-column>
      <el-table-column min-width="120px" label="分析日期">
        <template slot-scope="scope">
          <span>{{ scope.row.analysisDate }}</span></template>
      </el-table-column>
      <el-table-column min-width="100px" label="分析对象">
        <template slot-scope="scope">
          <span>{{ scope.row.analysisTarget }}</span></template>
      </el-table-column>
      <el-table-column min-width="100px" label="偏差">
        <template slot-scope="scope">
          <span>{{ scope.row.deflection }}</span></template>
      </el-table-column>
      <el-table-column min-width="100px" label="发生原因">
        <template slot-scope="scope">
          <span>{{ scope.row.reason }}</span></template>
      </el-table-column>
      <el-table-column min-width="100px" label="产生后果">
        <template slot-scope="scope">
          <span>{{ scope.row.consequence }}</span></template>
      </el-table-column>
      <el-table-column min-width="100px" label="建议措施">
        <template slot-scope="scope">
          <span>{{ scope.row.suggestMeasure }}</span></template>
      </el-table-column>
      <el-table-column min-width="100px" label="是否采纳">
        <template slot-scope="scope">
          <span v-if="scope.row.isAdopt==0" style="color:green;" >未处理</span>
          <span v-if="scope.row.isAdopt==3" style="color:blue;" >审批中...</span>
          <span v-if="scope.row.isAdopt==-1" style="color:orange;" >驳回</span>
          <span v-if="scope.row.isAdopt==1" style="color:red;" >采纳</span>
          <span v-if="scope.row.isAdopt==2" style="color:red;">不采纳</span></template>
      </el-table-column>
      <el-table-column min-width="100px" label="采纳计划/不采纳原因">
        <template slot-scope="scope">
          <span>{{ scope.row.planReason }}</span></template>
      </el-table-column>
      <el-table-column min-width="120px" label="审批时间">
        <template slot-scope="scope">
          <span>{{ scope.row.approvalDate }}</span></template>
      </el-table-column>
      <el-table-column min-width="100px" label="审批意见">
        <template slot-scope="scope">
          <span>{{ scope.row.approvalOpinion }}</span></template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="230">
        <template slot-scope="scope">
          <el-button v-if="scope.row.isAdopt==-1" type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button v-if="scope.row.isAdopt==-1" type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="150px" style="width: 400px; margin-left:50px;">
        <el-form-item v-if="hidden" label="编号" prop="id" readonly>
          <el-input v-model="temp.id" readonly />
        </el-form-item>
        <el-form-item label="节点范围" :required="!isRequired" prop="nodeRange">
          <el-input v-model="temp.nodeRange" />
        </el-form-item>
        <el-form-item label="分析日期" :required="!isRequired" prop="analysisDate">
           <el-date-picker v-model="temp.analysisDate" type="date" placeholder="请选择一个日期" />
        </el-form-item>
        <el-form-item label="分析对象" :required="!isRequired" prop="analysisTarget">
          <el-input v-model="temp.analysisTarget" />
        </el-form-item>
        <el-form-item label="偏差" :required="!isRequired" prop="deflection">
          <el-input v-model="temp.deflection" />
        </el-form-item>
        <el-form-item label="发生原因" :required="!isRequired" prop="reason">
          <el-input v-model="temp.reason" />
        </el-form-item>
        <el-form-item label="产生后果" :required="!isRequired" prop="consequence">
          <el-input v-model="temp.consequence" />
        </el-form-item>
        <el-form-item label="建议措施" :required="!isRequired" prop="suggestMeasure">
          <el-input v-model="temp.suggestMeasure" />
        </el-form-item>
        </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { adoptList, addAdopt, editAdopt, deleteAdopt} from '@/api/adopt'
import Sortable from 'sortablejs'
import { parseTime } from '@/utils'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { getuserbyemp } from '@/api/user'
import checkPermission from '@/utils/permission'
export default {
  name: 'Adopt',
  components: { },
  directives: { waves },
  data() {
    return {
      total: 0,
      list: null,
      listLoading: true,
      editList: null,
      nodeId1: null,
      sortable: null,
      nodeId: '',
      oldList: [],
      newList: [],

      // 参数条件
      listQuery: {
        // 获取URL参数
        nodeId: this.$route.query.nodeId
      },
      temp: {
        id: undefined,
        nodeId: '',
        nodeRange: '',
        analysisDate: '',
        analysisTarget: '',
        deflection: '',
        reason: '',
        consequence: '',
        suggestMeasure: '',
        isAdopt: '',
        planReason: '',
        approvalDate: '',
        approvalOpinion: '',
        status: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      //dialogDetailVisible: false,
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '明细'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'analysisDate', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
       },
      downloadLoading: false,
    }
  },
  created() { 
    this.fechData()
  },
  methods: {
    fechData() {
      adoptList(this.listQuery).then(response => {
        if (this.listQuery.nodeId === undefined) {
        alert('请先在危险性分析列表选择节点！')
        }
        this.listLoading = true
        this.list = response.ehsHazopAdoptList
        this.showList = response.ehsHazopAdoptList
        this.listLoading = false
        this.total = response.count
      })
    },
   
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const nodeId1 = this.$route.query.nodeId
          this.temp.author = 'vue-element-admin'
          const query =
          { nodeRange: this.temp.nodeRange,
            nodeId1: nodeId1,
            analysisDate: this.temp.analysisDate,
            analysisTarget: this.temp.analysisTarget,
            deflection: this.temp.deflection,
            reason: this.temp.reason,
            consequence: this.temp.consequence,
            suggestMeasure: this.temp.suggestMeasure, 
            isAdopt: this.temp.isAdopt,
            planReason: this.temp.planReason }
            addAdopt(query).then(() => {
            this.list.unshift(this.temp.nodeId1)
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: 'Created Successfully',
              type: 'success',
              duration: 2000
            })
            location.reload()
          })
        }
      })
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        nodeRange: '',
        analysisDate: '',
        analysisTarget: '',
        deflection: '',
        reason: '',
        consequence: '',
        suggestMeasure: '',
        isAdopt: '',
        planReason: '',
        approvalDate: '',
        approvalOpinion: '',
        status: ''
      }
    },

    showReviewer() {
    },

    handleFilter() {
      this.fechData()
    },

    handleEdit(row) {
      const row1 = JSON.parse(JSON.stringify(row))
      this.temp = Object.assign({}, row1)
     
      this.temp.timestamp = new Date(this.temp.timestamp)
      
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
      this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      if (this.dialogStatus === 'detail') {
        location.reload()
      } else if (this.dialogStatus === 'update') {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            const tempData = Object.assign({}, this.temp)
            const query1 = JSON.parse(JSON.stringify(tempData))
            tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
            const query = { id: query1.id,
              nodeRange: query1.nodeRange,
              analysisDate: query1.analysisDate,
              analysisTarget: query1.analysisTarget,
              deflection: query1.deflection,
              reason: query1.reason,
              consequence: query1.consequence,
              suggestMeasure: query1.suggestMeasure,
              operateTemperature: query1.operateTemperature,
              isAdopt: query1.isAdopt, 
              planReason: query1.planReason }
              editAdopt(query).then(response => {
              this.obj = response
              this.id = response.id
              this.nodeRange = response.nodeRange
            })
            // 刷新页面
            location.reload()
          }
        })
      }
    },
    
    handleDelete(row) {
      // eslint-disable-next-line no-lone-blocks
      {
        this.$confirm('确定删除?', 'Warning', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async() => {
            deleteAdopt(row.id).then(response => {
            })
            this.fechData()
            this.$message({
              type: 'success',
              message: 'Delete succed!'
            })
          })
          .catch(err => { console.error(err) })
      }
    },

    setSort() {
      const el = this.list[0]

      this.sortable = Sortable.create(el, {
        ghostClass: 'sortable-ghost',
        setData: function(dataTransfer) {
          dataTransfer.setData('Text', '')
        },
        onEnd: evt => {
          const targetRow = this.list.splice(evt.oldIndex, 1)[0]
          this.list.splice(evt.newIndex, 0, targetRow)

          const tempIndex = this.newList.splice(evt.oldIndex, 1)[0]
          this.newList.splice(evt.newIndex, 0, tempIndex)
        }
      })
    }
  }
}

</script>

<style>
.sortable-ghost{
  opacity: .8;
  color: #fff!important;
  background: #42b983!important;
}
</style>

<style scoped>
.icon-star{
  margin-right:2px;
}
.drag-handler{
  width: 20px;
  height: 20px;
  cursor: pointer;
}
.show-d{
  margin-top: 15px;
}
</style>